//
//  MLMainTabBar.swift
//  SinaWeibo
//
//  Created by 李亮亮 on 16/4/6.
//  Copyright © 2016年 Big Fish. All rights reserved.
//

import UIKit

class MLMainTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 添加子视图
        addSubview(plusBtn)
        
    }
    
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 获取subView中 tabBarButton的frame  把中间的位置空出来
    override func layoutSubviews() {
        super.layoutSubviews()
       
        let itemW = self.frame.width / 5
        let itemH = self.frame.height
        // 按钮的大小
        let rect = CGRectMake(0, 0, itemW, itemH)
        var index: CGFloat = 0
        // 遍历子视图
        for subView in subviews {
            // UITabBarButton 是私有类
//            print(subView)
            if subView .isKindOfClass(NSClassFromString("UITabBarButton")!) {
                // 按钮的frame变化
                subView.frame = CGRectOffset(rect, itemW * index, 0)
                
//                if index == 1 {
//                    index++
//                }
//                index++
                index += (index == 1) ? 2 : 1
                
            }
        }
        
        // 设置加号按钮的frame
        plusBtn.frame = CGRectOffset(rect, 2 * itemW, 0)
        
//        plusBtn.frame = CGRectOffset(rect, 2 * itemW, -10)
        // 移动到最顶层
//        bringSubviewToFront(plusBtn)
        
    }
    
    // 懒加载加号按钮
    lazy var plusBtn: UIButton = {
        // alloc | init 实例化得到的按钮就是一个自定义的按钮
        let btn = UIButton()
        
        // 设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        // 设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        return btn
    }()
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
